package com.sdl.timesblogmp.model.vo;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.util.HashMap;

/**
 * @author sdl
 * 统一返回
 * @date 2020/5/12 12:44 下午
 * @description
 */
@ApiModel(description = "返回接口类")
public class ApiResult extends HashMap<String,Object> {
    /** 状态码 */
    @ApiModelProperty(value = "状态码")
    public static final String CODE_TAG = "code";
    /** 返回内容 */
    @ApiModelProperty(value = "返回内容")
    public static final String MSG_TAG = "msg";
    /** 数据对象 */
    @ApiModelProperty(value = "数据对象")
    public static final String DATA_TAG = "data";

    public ApiResult() {
    }
    public ApiResult(Integer code,String msg,Object data) {
        super.put(CODE_TAG,code);
        super.put(MSG_TAG,msg);
        if(data!=null){
            super.put(DATA_TAG,data);
        }
    }
    /**
     * 成功消息
     * @param code
     * @param msg
     * @param data
     * @return
     */
    public static ApiResult success(Integer code,String msg,Object data){
        return new ApiResult(code,msg,data);
    }
    public static ApiResult success(String msg,Object data){
        return success(200,msg,data);
    }
    public static ApiResult success(String msg){
        return success(200,msg,null);
    }
    public static ApiResult success(Object data){
        return success(200,"操作成功",data);
    }
    public static ApiResult success(){
        return success("操作成功");
    }
    /**
     * 失败
     * @param msg
     * @param data
     * @return
     */
    public static ApiResult error(Integer code,String msg,Object data){
        return new ApiResult(code,msg,data);
    }
    public static ApiResult error(Integer code,String msg){
        return  error(code,msg,null);
    }
    public static ApiResult error(String msg){
        return  error(500,msg,null);
    }
    public static ApiResult error(Object data){
        return  error(500,"操作失败",null);
    }
    public static ApiResult error(){
        return error("操作失败");
    }
}
